<!-- eslint-disable no-unused-vars -->
<template>
	<xh-list-container
		ref="pageList"
		title="绣花收回"
		:request="request"
		@clickAdd="handleClickAdd"
		@request="handleRequest"
		:labelWidth="85"
		:columns="columns"
		:hideAddBtn="hideAddBtn"
		rowKey="embroideryId"
		@changeSearchSelect="handleChangeSearchSelect"
	>
		<template v-slot="scope">
			<xh-list-item
				v-for="(item, index) in scope.data"
				:item-data="item"
				:index="index"
				:key="item.embroideryId"
				:columns="columns"
				@click="handleClickItem(item)"
				@clickBtns="handleClickBtns"
				:buttons="getButtons(item, index)"
			/>
		</template>
		<template #summary>
			<xh-summary-box
				:summaryData="applySummaryData"
				:labelList="[
					{ label: '裁片数汇总', key: 'cutPieceSum' },
					{ label: '绣片数汇总', key: 'embroideryPieceSum' },
				]"
			/>
		</template>
	</xh-list-container>
</template>

<script>
import { EmbroideryTypeEnum, AuditStatusEnum } from '@/utils/constant';
import {
	getEmbroideryRetractionList,
	deleteEmbroideryRetractionRecord,
	getRetractionCuttingList,
	getRetractionRecordSummary,
	getEmbroideryProductList,
	embroideryRetractionAudit,
	copyEmbroideryRetractionRecord,
} from '@/api/purchase/pickplanbill.js';
import { formatSelectList } from '@/utils/business.js';
import AppStorage from '@/utils/appStorage';
import { Role } from '@/utils/constant';

// 导入组件
export default {
	name: 'embroideryApply',
	data() {
		return {
			productList: [],
			cuttingList: [], // 裁片编码list
			request: getEmbroideryRetractionList,
			applySummaryData: {},
			hideAddBtn: false,
			columns: [
				{
					label: '收回时间',
					key: 'takeBackDate',
					searchOrder: 1,
					search: true,
					width: '100%',
					searchType: 'daterange',
				},
				{
					label: '产品名称',
					key: 'productName',
					search: true,
					valueMapList: [],
				},
				{
					label: '类型',
					key: 'type',
					search: false,
					valueMapList: EmbroideryTypeEnum,
				},
				{
					label: '裁片编码',
					key: 'cuttingCode',
					searchOrder: 2,
					search: true,
					valueMapList: [],
				},
				{
					label: '审核状态',
					key: 'auditStatus',
					search: false,
					valueMapList: AuditStatusEnum,
				},
			],
		};
	},
	onShow() {
		this.$refs.pageList?._query();
	},
	created() {
		this.fetchProductList();
		this.userInfo = AppStorage.get('userInfo');
		this.request = getEmbroideryRetractionList;
		const { roleKeys } = this.userInfo;
		this.hideAddBtn =
			roleKeys.includes(Role.inspector) ||
			roleKeys.includes(Role.warehouseStaff);
	},
	methods: {
		handleClickBtns(btnKey, item, index) {
			if (btnKey === 'delete') {
				this.handleDeleteItem(item, index);
			} else if (btnKey === 'pass') {
				this.$nav.navigateTo(
					'/subProductionManage/embroideryWithdraw/checkPage',
					item,
					false
				);
			} else if (btnKey === 'copy') {
				// eslint-disable-next-line no-unused-vars
				const { embroideryId, params, ...rest } = item;
				copyEmbroideryRetractionRecord({
					...rest,
					auditStatus: '3',
				}).then(() => {
					this.$refs.pageList?._query();
				});
			} else if (btnKey === 'checkDetail') {
				this.$nav.navigateTo(
					'/subProductionManage/embroideryWithdraw/checkDetailPage',
					item,
					false
				);
			} else {
				const statusObj = {
					pass: '1',
					return: '2',
					store: '7',
					stockReturn: '6',
				};
				let status = statusObj[btnKey];
				const { auditStatus } = item;
				if (auditStatus === '3' && btnKey === 'pass') {
					status = 4;
				}
				this.fetchEmbroideryRetractionAudit(item, status);
			}
		},
		// 删除
		// eslint-disable-next-line no-unused-vars
		handleDeleteItem(item, index) {
			const id = item.embroideryId;
			this.$modal.confirm('确定删除吗？').then(() => {
				deleteEmbroideryRetractionRecord(id).then(() => {
					this.$modal.msgSuccess('删除成功！');
					this.$refs.pageList?._query();
				});
			});
		},
		withAdmin(role, roleKeys) {
			return roleKeys.includes(role) || roleKeys.includes(Role.admin);
		},
		getButtons(item) {
			const { auditStatus } = item;
			const { roleKeys } = this.userInfo;
			const checkBtn = { text: '检验', key: 'pass', color: 'primary' };
			const checkDetailBtn = {
				text: '检验详情',
				key: 'checkDetail',
				color: 'primary',
			};
			const copyBtn = { text: '复制', key: 'copy', color: 'primary' };
			const deleteBtn = { text: '删除', key: 'delete', color: 'error' };
			const btnsArr = [];
			if (['2', '6'].includes(auditStatus) && !this.hideAddBtn) {
				btnsArr.push(copyBtn);
			}
			// 待检，质检人员， 按钮:【检验】
			if (this.withAdmin(Role.inspector, roleKeys) && auditStatus === '0') {
				btnsArr.push(checkBtn);
			}
			// 合格&仓储人员，显示【入库】【退库】【检验详情】
			if (
				this.withAdmin(Role.warehouseStaff, roleKeys) &&
				['1', '4'].includes(auditStatus)
			) {
				btnsArr.push({ text: '入库', key: 'store', color: 'primary' });
				btnsArr.push({ text: '退库', key: 'stockReturn', color: 'error' });
			}
			btnsArr.push(checkDetailBtn);
			if (
				this.withAdmin(Role.inspector, roleKeys) &&
				['0', '3'].includes(auditStatus)
			) {
				btnsArr.push(deleteBtn);
			}
			return btnsArr;
		},
		handleRequest(params) {
			getRetractionRecordSummary({ ...params }).then((res) => {
				const { data } = res;
				this.applySummaryData = data || {};
			});
		},
		fetchProductList() {
			getEmbroideryProductList().then((res) => {
				const { rows } = res;
				const newData = Array.from(new Set(rows));
				this.productList = newData.map((value) => ({ text: value, value }));
				this.columns[1].valueMapList = this.productList;
			});
		},
		fetchEmbroideryRetractionAudit(item, auditStatus) {
			embroideryRetractionAudit({
				...item,
				auditStatus,
			}).then(() => {
				this.$modal.msgSuccess('操作成功！');
				this.$refs.pageList?._query();
			});
		},
		handleClickAdd() {
			this.$nav.navigateTo('/subProductionManage/embroideryWithdraw/addForm');
		},
		handleClickItem(item) {
			if (!this.hideAddBtn) {
				this.$nav.navigateTo(
					'/subProductionManage/embroideryWithdraw/detailPage',
					item,
					false
				);
			}
		},
		handleChangeSearchSelect(option, columnItem) {
			const { label } = columnItem;
			if (label === '产品名称') {
				this.fetchCuttingList(option.text);
			}
		},
		fetchCuttingList(productName) {
			getRetractionCuttingList({ productName }).then((res) => {
				const { rows } = res;
				this.cuttingList = formatSelectList(
					rows.filter((item) => item.cuttingCode),
					'cuttingCode'
				);
			});
		},
	},
};
</script>

<style scoped lang="scss"></style>
